package JavaSE.ThreadLearning.Learning;

/**
 *  学习 Thread多线程
 */
public class Demo {
    public static void main(String[] args) throws InterruptedException {
        Boom zs = new Boom("张三");
        Boom ls = new Boom("李四");
        Clap clap = new Clap();
        ls.start();
        zs.start();
        // join()方法会使所在的线程(此时是主线程main)等待
        // 知道规定的时间后才继续执行
        // 如果没有参数则等待直到调用该方法的线程执行完毕
        zs.join(3000);

        clap.start();
        // 可发现，当main中仍有非守护线程时，main不会结束
        System.out.println("--------------即将main结束------------");
        return;
    }
}

class Boom extends Thread {
    String name;
    Boom(String name) {
        super(name);
        this.name = name;
    }
    public void boom() {
        for (int i = 0; i < 10; i++) {
            System.out.println(this.name+"的第"+i+"次爆炸");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        boom();
    }
}

class Clap extends Thread {
    public static void clap() {
        for (int i = 0; i < 10; i++) {
            System.out.println("第" + i + "次鼓掌");
        }
    }

    @Override
    public void run() {
        clap();
    }
}
